package subject.tengxun.middle;

import java.util.Arrays;

/**
 * 功能：
 * 作者：yedou
 * 日期：2025/3/24 21:13
 */
public class productExceptSelf {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4};

        int n = nums.length;
        int[] answer = new int[n];

        // 计算左边乘积
        answer[0] = 1;
        for (int i = 1; i < n; i++) {
            answer[i] = answer[i - 1] * nums[i - 1];
        }

        // 计算右边乘积并与左边乘积相乘
        int right = 1;
        for (int i = n - 1; i >= 0; i--) {
            answer[i] = answer[i] * right;
            right *= nums[i];
        }
        System.out.println(Arrays.toString(answer));
    }

}